{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Inheritance\n", "\n", "\n", "In OOP, one can also create a class that is based on another class. \n", "\n", "* The parent class is called the **superclass**\n", "* The new, child class is called the **subclass**\n", "\n", "In this example, we make a **base class** `Animal` to serve as the superclass of a subclass `Dog`.\n", "\n", "We say that a Dog **is-a** Animal.\n", "\n", "For **has-a** relationships, we make those properties of the class (like has-a name)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added class Animal\n", "\n" ] } ], "source": [ "class Animal {\n", " public void speak() {\n", " printf(\"I am an animal\\n\");\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable animal of type Animal with initial value Animal@44e81672\n", "\n" ] } ], "source": [ "Animal animal = new Animal();" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I am an animal\n", "\n" ] } ], "source": [ "animal.speak();" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added class Dog\n", "\n" ] } ], "source": [ "class Dog extends Animal {\n", " public void speak() {\n", " printf(\"woof!\\n\");\n", " } \n", "}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable dog of type Dog with initial value Dog@2471cca7\n", "\n" ] } ], "source": [ "Dog dog = new Dog();" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "woof!\n", "\n" ] } ], "source": [ "dog.speak();" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable whatisthis of type Animal with initial value Dog@2471cca7\n", "\n" ] } ], "source": [ "Animal whatisthis = dog;" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "woof!\n", "\n" ] } ], "source": [ "whatisthis.speak();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Dog object gets the message \"speak\" and it has a method for that. \n", "\n", "This is called **Polymorphism**.\n", "\n", "*See Page 19 of your textbook for more details.*" ] } ], "metadata": { "kernelspec": { "display_name": "Java 9", "language": "java", "name": "java9" }, "language_info": { "file_extension": ".class", "mimetype": "application/java-vm", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }